<template>
  <div class="about">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>人事管理</el-breadcrumb-item>
      <el-breadcrumb-item>工资管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索与添加按钮 -->
      <el-form :inline="true" :model="info" ref="info">
        <el-form-item prop="jobNumber">
          <el-input
            style="width: 160px"
            v-model="info.jobNumber"
            placeholder="请输入工号"
            clearable
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="startTime">
          <el-date-picker
            style="width: 160px"
            v-model="info.startTime"
            type="month"
            start-placeholder="开始日期"
            placeholder="请选择开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="endTime">
          <el-date-picker
            style="width: 160px"
            v-model="info.endTime"
            type="month"
            end-placeholder="结束日期"
            placeholder="请选择结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-form-item>
            <el-button
              type="success"
              @click="resetForm('info')"
              size="medium"
              round
              >重置</el-button
            >
          </el-form-item>
          <el-button type="primary" @click="getUserList" size="medium" round
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <el-row>
        <el-col :span="4"
          ><el-button type="primary" @click="addDialogVisible = true"
            >添加工资信息</el-button
          ></el-col
        >
      </el-row>
      <!-- 工资列表 -->
      <el-table border stripe :data="userList">
        <el-table-column label="序号" type="index"> </el-table-column>
        <el-table-column label="工号" prop="jobNumber"> </el-table-column>
        <el-table-column label="日期" prop="date"> </el-table-column>
        <el-table-column label="税前工资" prop="grossPay"> </el-table-column>
        <el-table-column label="五险扣除" prop="fiveRiskMoney">
        </el-table-column>
        <el-table-column label="公积金扣除" prop="accumulationFund">
        </el-table-column>
        <el-table-column label="个税金额" prop="personTax"> </el-table-column>
        <el-table-column label="基本工资" prop="basicSalary"> </el-table-column>
        <el-table-column label="应发金额" prop="payableAmount">
        </el-table-column>
        <el-table-column label="实发金额" prop="paidAmount"> </el-table-column>
        <el-table-column label="操作" width="145px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="showEditDialog(scope.row.id)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="removeId(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="info.currentPage"
        :page-sizes="[1, 2, 10, 100]"
        :page-size="info.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 编辑工资信息 -->
      <el-dialog
        title="更新编辑工资信息"
        :visible.sync="editDialogVisible"
        width="29%"
        @close="editDialogClosed"
      >
        <el-form
          :model="editForm"
          :rules="editFormRules"
          ref="editFormRef"
          label-width="100px"
        >
          <el-form-item label="工号" prop="jobNumber">
            <el-input v-model="editForm.jobNumber"></el-input>
          </el-form-item>
          <el-form-item label="日期" prop="date">
            <el-date-picker
              v-model="editForm.date"
              type="month"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="税前工资" prop="grossPay">
            <el-input v-model="editForm.grossPay"></el-input>
          </el-form-item>
          <el-form-item label="五险扣除" prop="fiveRiskMoney">
            <el-input v-model="editForm.fiveRiskMoney"></el-input>
          </el-form-item>
          <el-form-item label="公积金扣除" prop="accumulationFund">
            <el-input v-model="editForm.accumulationFund"></el-input>
          </el-form-item>
          <el-form-item label="个税金额" prop="personTax">
            <el-input v-model="editForm.personTax"></el-input>
          </el-form-item>
          <el-form-item label="基本工资" prop="basicSalary">
            <el-input v-model="editForm.basicSalary"></el-input>
          </el-form-item>
          <el-form-item label="应发金额" prop="payableAmount">
            <el-input v-model="editForm.payableAmount"></el-input>
          </el-form-item>
          <el-form-item label="实发金额" prop="paidAmount">
            <el-input v-model="editForm.paidAmount"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editInfo">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 添加工资信息对话框 -->
      <el-dialog
        title="添加工资信息"
        :visible.sync="addDialogVisible"
        width="35%"
        @close="addDialogClosed"
      >
        <el-form
          :model="addForm"
          :rules="addFormRules"
          ref="addFormRef"
          label-width="100px"
        >
          <el-form-item label="部门" prop="department">
            <el-select v-model="addForm.department">
              <el-option
                v-for="item in bum"
                :key="item.id"
                :label="item.name"
                :value="item"
                @click.native="getName(item)"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="工号" prop="jobNumber">
            <el-select v-model="addForm.jobNumber">
              <el-option
                v-for="item in jobName"
                :key="item.id"
                :label="item.name"
                :value="item"
                @click.native="getMoney(item)"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="日期" prop="date">
            <el-date-picker
              v-model="addForm.date"
              type="month"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="税前工资" prop="grossPay">
            <el-select v-model="addForm.grossPay">
              <el-option v-model="jobMoney.grossPay"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="五险扣除" prop="fiveRiskMoney">
            <el-select v-model="addForm.fiveRiskMoney">
              <el-option v-model="jobMoney.fiveRiskMoney"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="公积金扣除" prop="accumulationFund">
            <el-select v-model="addForm.accumulationFund">
              <el-option v-model="jobMoney.accumulationFund"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="个税金额" prop="personTax">
            <el-select v-model="addForm.personTax">
              <el-option v-model="jobMoney.personTax"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="基本工资" prop="basicSalary">
            <el-select v-model="addForm.basicSalary">
              <el-option v-model="jobMoney.basicSalary"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="绩效扣除" prop="meritSalary">
            <el-select v-model="addForm.meritSalary">
              <el-option v-model="jobMoney.meritSalary"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="应发金额" prop="payableAmount">
            <el-select v-model="addForm.payableAmount">
              <el-option v-model="jobMoney.payableAmount"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="实发金额" prop="paidAmount">
            <el-select v-model="addForm.paidAmount">
              <el-option v-model="jobMoney.paidAmount"> </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addBum">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    // //校验电话号码
    // var checkMobile = (rule, value, cb) => {
    //   //验证手机号码的正则表达式
    //   const regMobile = /^[1][3,4,5,7,8][0-9]{9}$/;
    //   if (regMobile.test(value)) {
    //     return cb();
    //   }
    //   cb(new Error("请输入合法的电话号码"));
    // };
    return {
      info: {
        currentPage: 1,
        size: 4,
        jobNumber: "",
        startTime: "",
        endTime: "",
      },
      userList: [],
      total: 0,
      //编辑工资信息对话框隐藏
      editDialogVisible: false,
      //查询到的工资信息对象
      editForm: {},
      //编辑工资信息规则
      editFormRules: {
        jobNumber: [{ required: true, message: "请输入工号", trigger: "blur" }],
        date: [{ required: true, message: "请输入日期", trigger: "blur" }],
        meritId: [{ required: true, message: "请输入绩效", trigger: "blur" }],

        basicSalary: [
          { required: true, message: "请输入基本工资", trigger: "blur" },
        ],
        grossPay: [
          { required: true, message: "请输入税前工资", trigger: "blur" },
        ],
        fiveRiskMoney: [
          { required: true, message: "请输入五险扣除金额", trigger: "blur" },
        ],
        accumulationFund: [
          { required: true, message: "请输入公积金扣除金额", trigger: "blur" },
        ],
        personTax: [
          { required: true, message: "请输入个税金额", trigger: "blur" },
        ],
        payableAmount: [
          { required: true, message: "请输入应发金额", trigger: "blur" },
        ],
        paidAmount: [
          { required: true, message: "请输实发金额", trigger: "blur" },
        ],
      },
      // 添加工资对话框隐藏
      addDialogVisible: false,
      //添加工资的表单数据
      addForm: {
        department: "",
        jobNumber: "",
        date: "",
        basicSalary: "",
        grossPay: "",
        fiveRiskMoney: "",
        accumulationFund: "",
        personTax: "",
        payableAmount: "",
        paidAmount: "",
        meritId: "",
        meritSalary: "",
      },
      //添加表单的规则验证对象
      addFormRules: {},
      bum: [],
      jobName: [],
      jobMoney: [],
    };
  },
  created() {
    this.getUserList();
    this.department();
  },
  methods: {
    //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //获取工资信息
    async getUserList() {
      await this.$http
        .get("admin/selectSalary", {
          params: this.info,
        })
        .then((res) => {
          console.log(res);
          if (res.data.flag !== true) {
            return this.$message.error("获取工资信息失败!");
          }
          this.userList = res.data.rows;
          this.total = res.data.total;
        });
    },
    //监听信息条数
    handleSizeChange(newSize) {
      // console.log(newSize);
      this.info.size = newSize;
      this.getUserList();
    },
    //监听页数
    handleCurrentChange(newPage) {
      // console.log(newPage);
      this.info.currentPage = newPage;
      this.getUserList();
    },
    //更新编辑工资信息打卡
    async showEditDialog(id) {
      console.log(id);
      const { data: res } = await this.$http.get("admin/selectSalaryById", {
        params: { id },
      });
      if (res.flag !== true) {
        return this.$message.error("获取工资信息失败!");
      }
      console.log(res);

      this.editForm = res.data;
      this.editDialogVisible = true;
    },
    // 监听更新工资对话框的关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
    //更新工资信息并提交
    editInfo() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("admin/updateSalary", {
          id: this.editForm.id,
          jobNumber: this.editForm.jobNumber,
          department: this.editForm.department,
          employeeName: this.editForm.employeeName,
          phone: this.editForm.phone,
          date: this.editForm.date,
          basicSalary: this.editForm.basicSalary,
          grossPay: this.editForm.grossPay,
          fiveRiskMoney: this.editForm.fiveRiskMoney,
          accumulationFund: this.editForm.accumulationFund,
          personTax: this.editForm.personTax,
          payableAmount: this.editForm.payableAmount,
          paidAmount: this.editForm.paidAmount,
          meritId: this.editForm.meritId,
        });
        if (res.flag !== true) {
          return this.$message.error("更新工资信息失败");
        }
        //关闭对话框
        this.editDialogVisible = false;
        //提示更新工资信息成功
        this.$message.success("更新工资信息成功");
        //获取新的更新工资信息
        this.getUserList();
      });
    },
    //监听添加工资信息表单的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    //添加工资管理信息
    addBum() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        //可以发起添加工资信息的网络请求
        const { data: res } = await this.$http.post(
          "admin/insertSalary",
          this.addForm
        );
        if (res.flag !== true) {
          return this.$message.error("添加工资信息失败!");
        }
        this.$message.success("添加工资信息成功!");
        //隐藏添加工资信息的对话框
        this.addDialogVisible = false;
        //重新获取工资信息列表数据
        this.getUserList();
      });
    },
    //根据id删除工资信息
    async removeId(id) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该条工资信息, 是否继续?",
        "删除工资信息",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);

      if (confirmResult !== "confirm") {
        return this.$message.info("已经取消删除！");
      }
      const { data: res } = await this.$http.post("admin/deleteSalary/", {
        id: id,
      });
      console.log(res);

      if (res.flag !== true) {
        return this.$message.error("删除工资信息失败");
      }
      this.$message.success("删除工资信息成功");
      this.getUserList();
    },
    //获取部门信息
    async department() {
      const { data: res } = await this.$http.get(
        "organization/selectDepartmentNames"
      );
      this.bum = res.data;
      console.log(res);
    },
    //获取员工工号
    async getName(value) {
      console.log(value);
      const { data: res } = await this.$http.get(
        "admin/selectJobNumberByDepartmentName",
        {
          params: { departmentName: value },
        }
      );

      this.jobName = res.data;
      console.log(this.jobName);
    },
    //获取工资信息
    async getMoney(value) {
      console.log(value);
      const { data: res } = await this.$http.get(
        "admin/selectSalaryInfoByJobNum",
        {
          params: { jobNumber: value },
        }
      );

      this.jobMoney = res.data;
      console.log(this.jobMoney);
    },
  },
};
</script>

<style></style>
